<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教师" prop="teacherId">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入教师"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="courseList">
      <el-table-column type="selection" width="75" align="center" />
      <el-table-column type="index" label="序号" align="center" width="75"/>
<!--      <el-table-column label="课程描述" align="center"  prop="des">
        <template slot-scope="scope">
          <el-tooltip :content="scope.row.des" placement="top">
            <p class="showOverTooltip">{{scope.row.des}}</p>
          </el-tooltip>
        </template>
      </el-table-column>-->
      <el-table-column label="课程名称" align="center" prop="courseName" />
      <el-table-column label="分数" align="center" prop="score" />
      <el-table-column label="教师" align="center" prop="teachName" />
      <el-table-column label="评价" align="center" prop="teachScore" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="evaForStudent(scope.row)"
            v-if="scope.row.teachScore==null"
          >为教师评价</el-button>
          <el-button
            v-else
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="evaForStudentUpdate(scope.row)"
          >修改教师评价</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog
      @close="closeScore"
      :title="'为教师：'+(courseItem==null?'‘无’':courseItem.teachName)+'评价'"
      :visible.sync="choseVisible"
      width="30%"
      center>
      <span></span>
      <el-form :model="ruleForm" ref="ruleForm" :rules="rules2" label-width="80px">
        <el-form-item label="评分" prop="teachSocre">
          <el-input-number v-model="ruleForm.teachScore" :min="1" :max="10" label="请输入评分"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="choseVisible = false">取 消</el-button>
        <el-button type="primary" @click="evaluate">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import {getAllBySelect,getTeacherRate, getStudentScore, rateTeach} from "@/api/courseSystem/student";
import {getInfo} from "@/api/login";
import {getStudentRate, rateStudent, rateTeacherScore} from "@/api/courseSystem/teach";

export default {
  name: "studentScore",
  data() {
    return {
      ruleForm:{
        teachScore:null
      },
      courseItem:null,
      choseVisible:false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程表格数据
      courseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        des: null,
        courseName: null,
        teacherId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      rules2: {
        teachScore: [
          { required: true, message: '请输入您的评分', trigger: 'blur' },
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    closeScore(){
      this.courseItem = null;
      this.queryParams.teachSocre = null;
    },
    evaForStudent(row){
      this.courseItem = JSON.parse(JSON.stringify(row));
      this.ruleForm.teachScore = null;
      this.choseVisible = true;
    },
    // 回显评价
    evaForStudentUpdate(row){
      this.courseItem = JSON.parse(JSON.stringify(row))
      console.log(row)
      this.ruleForm.teachScore = null;
        getTeacherRate(row.teacherId,row.studentId).then(res=>{
          if (res.code==200){
            this.ruleForm.teachScore = res.data.score;
            this.choseVisible = true;
          }else {
            this.$message.warning('查询教师：'+row.teachName+'的评价出错啦')
          }
        })
    },
    // 增、改 提交表单
    evaluate(){
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          if (this.courseItem!=null){
            if(this.courseItem.teachScore==null){//新增
              console.log(11)
              getInfo().then(response=>{
                // teachscore有值为新增，否则修改
                rateStudent({studentId:response.user.userName,
                  teachId:this.courseItem.studentId,
                  score:this.ruleForm.teachScore}).then(res=>{
                  if (res.code==200){
                    this.$message.success('评价成功')
                    this.choseVisible = false;
                    this.getList();
                  }else {
                    this.$message.error('评价失败啦')
                  }
                })
              })
            }else {//修改
              getInfo().then(response=>{
                console.log(this.courseItem)
                rateTeacherScore({teachId:this.courseItem.teacherId,
                  studentId:response.user.userName,score:this.ruleForm.teachScore}).then(res=>{
                  if (res.code==200){
                    this.$message.success('修改成功')
                    this.choseVisible = false;
                    this.getList()
                  }else {
                    this.$message.error('修改失败啦')
                  }
                })
              })
            }
          }else {
            this.$message.warning('未选择学生')
          }
        }
      })
    },
    getList() {
      this.loading = true;
      getInfo().then(res=>{
        getStudentScore(res.user.userName,this.queryParams).then(response=>{
          if (response.code!=200){
            this.$message.error("查询出错啦")
          }else {
            this.courseList = response.data.rows
            this.total = response.data.total
            this.loading = false;
            this.$message.success('查询成功')
          }
        })
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        courseId: null,
        des: null,
        courseName: null,
        teacherId: null,
        teachType: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程";
    },

  }
};
</script>
<style scoped>
.showOverTooltip{
  display:-webkit-box;
  text-overflow:ellipsis;
  overflow:hidden;
  /*这里是3行*/
  -webkit-line-clamp: 3;
  -webkit-box-orient:vertical;
}
</style>
